import {toLogin} from './../../api/index'
let actions = {
    //获取登录信息
    async loginMessage({ commit }, { username, pwd }) {
        let result = await toLogin(username, pwd)
        if (result.errno != 0) {
            //出错
            return Promise.reject(result.errmsg)
        }else{
            //成功---存储token,跳转
            localStorage.setItem('user', result.data.token)
            localStorage.setItem('userInfo',JSON.stringify(result.data.userInfo))
            commit('login_message',result.data.userInfo)
            return '成功'
        }
    },
    //清除token
    clearToken({commit}){
        commit('clear_token')
    }
}
let mutations = {
    login_message(state,userInfo){
        state.userInfo = userInfo
    },
    clear_token(state){
        //重置用户信息和token
        state.token = ''
        state.userInfo = {}
        localStorage.removeItem('user')
        localStorage.removeItem('userInfo')
    }
}
let state = {
    token:localStorage.getItem('user'),
    userInfo:{}
}
let getters = {}
export default {
    actions,
    mutations,
    state,
    getters
}